/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package nl.xs4all.dylanschell.deckprinter.services;

import com.google.inject.Singleton;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @author d.schell
 */
@Singleton
public class DeckPrinterConfigImpl implements DeckPrinterConfig
{
    private final String userHome;
    private final String fileSeparator;
    private File myFolder;
    private boolean printBasicLand = true;
    private File deckFolder;
    private File cacheFolder;
    private File setFolder;

    public DeckPrinterConfigImpl()
    {
        userHome = System.getProperty("user.home");
        fileSeparator = System.getProperty("file.separator");
        load();
    }

    public synchronized File getDeckPrinterFolder()
    {
        if (myFolder == null)
        {
            myFolder = new File(userHome + fileSeparator + ".deckprinter");
            if (!myFolder.isDirectory())
            {
                myFolder.mkdirs();
            }
        }
        return myFolder;
    }

    public boolean isPrintBasicLand()
    {
        return printBasicLand;
    }

    public void setPrintBasicLand(boolean printBasicLand)
    {
        this.printBasicLand = printBasicLand;
        save();
    }

    public void save()
    {
        File configFile = new File(getDeckPrinterFolder(), "deckprinter.properties");
        Properties properties = new Properties();
        properties.put("printBasicLand", Boolean.toString(isPrintBasicLand()));
        properties.put("deckFolder", getDeckFolder().toString());
        properties.put("cacheFolder", getCacheFolder().toString());
        FileOutputStream os;
        try
        {
            os = new FileOutputStream(configFile);
            try
            {
                properties.store(os, "Deck Printer Configuration");
            } catch (IOException ex)
            {
                Logger.getLogger(DeckPrinterConfigImpl.class.getName()).log(Level.SEVERE, null, ex);
            } finally
            {
                try
                {
                    os.close();
                } catch (IOException ex)
                {
                    Logger.getLogger(DeckPrinterConfigImpl.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        } catch (FileNotFoundException ex)
        {
            Logger.getLogger(DeckPrinterConfigImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void load()
    {
        FileInputStream fis = null;
        try
        {
            File configFile = new File(getDeckPrinterFolder(), "deckprinter.properties");
            if (configFile.isFile())
            {
                Properties properties = new Properties();
                fis = new FileInputStream(configFile);
                properties.load(fis);
                printBasicLand = Boolean.parseBoolean(properties.getProperty("printBasicLand", Boolean.toString(true)));
                String prop = properties.getProperty("deckFolder", null);
                if (prop != null)
                {
                    deckFolder = new File(prop);

                }
                prop = properties.getProperty("cacheFolder", null);
                if (prop != null)
                {
                    cacheFolder = new File(prop);
                }
            }
        } catch (IOException ex)
        {
            Logger.getLogger(DeckPrinterConfigImpl.class.getName()).log(Level.SEVERE, null, ex);
        } finally
        {
            try
            {
                if (fis != null)
                {
                    fis.close();
                }
            } catch (IOException ex)
            {
                Logger.getLogger(DeckPrinterConfigImpl.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public File getDeckFolder()
    {
        if (deckFolder == null)
        {
            deckFolder = new File(getDeckPrinterFolder(), "decks");
            if (!deckFolder.exists())
            {
                deckFolder.mkdirs();
            }
        }
        return deckFolder;
    }

    public void setDeckFolder(File deckFolder)
    {
        this.deckFolder = deckFolder;
        save();
    }

    public void setCacheFolder(File file)
    {
        this.cacheFolder = file;
        save();
    }

    public File getCacheFolder()
    {
        if (cacheFolder == null)
        {
            cacheFolder = new File(getDeckPrinterFolder(), "cache");
        }
        return cacheFolder;
    }

    public File getSetFolder()
    {
        if (setFolder == null)
        {
            setFolder = new File(getDeckPrinterFolder(), "OCTGN");
            if (!setFolder.exists())
            {
                setFolder.mkdirs();
            }
        }
        return setFolder;
    }
}
